/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.facades.persistence;

import com.sdd.entities.Comment;
import com.sdd.entities.Comment_;
import com.sdd.entities.Product;
import com.sdd.entities.Product_;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author Carlos
 */
@Stateless
public class CommentPersistenceFacade extends AbstractPersistenceFacade<Comment>{

    @PersistenceContext(unitName = "SDDStore-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CommentPersistenceFacade() {
        super(Comment.class);
    }
    
    public List<Comment> getCommentsByProduct(Product p){
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Comment> query = builder.createQuery(Comment.class);
        Root<Comment> r = query.from(Comment.class);
        query.select(r).where(builder.equal(r.get(Comment_.product).get(Product_.id),p.getId()));
        return getEntityManager().createQuery(query).getResultList();
    }

}
